package com.cj.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.cms.entity.Article;
import com.cj.cms.entity.Channel;
import com.cj.cms.entity.WebSite;
import com.cj.cms.service.ArticleManager;
import com.cj.cms.service.WebSiteManager;
import com.cj.common.orm.Page;
import com.cj.common.orm.PropertyFilter;
import com.cj.common.orm.PropertyFilter.MatchType;
import com.cj.common.orm.hibernate.HibernateWebUtils;
import com.cj.common.web.struts2.BaseActionSupport;
import com.cj.common.web.struts2.Struts2Utils;

public class SearchAction extends BaseActionSupport{

	private static WebSite webSite;
	@Autowired
	private WebSiteManager webSiteManager;
	
	private static Channel entity;
//	private String title;
	private Long channelId;
	@Autowired
	private ArticleManager articleManager;

	protected Page<Article> page = new Page<Article>(20,"isTop,modifyTime",Page.DESC + "," + Page.DESC);
	@Override
	public String execute() throws Exception {
		try {
			HttpServletRequest request = Struts2Utils.getRequest();
			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
//			filters.add(new PropertyFilter("title",title,MatchType.LIKE));
			if(channelId !=  null)
				filters.add(new PropertyFilter("channel.id",channelId,MatchType.EQ));
			page = articleManager.search(page, filters);
			return SUCCESS;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	
	public WebSite getWebSite() {
		if(webSite == null)
			webSite = webSiteManager.getAll().get(0);
		return webSite;
	}
	public void setWebSite(WebSite webSite) {
		this.webSite = webSite;
	}

	public Channel getEntity() {
		if(entity == null){
			entity = new Channel();
			entity.setName("搜索");
		}
		return entity;
	}

	public void setEntity(Channel entity) {
		SearchAction.entity = entity;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public Page<Article> getPage() {
		return page;
	}

	public void setPage(Page<Article> page) {
		this.page = page;
	}
	

}
